package zzzhc.image.ui;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.commons.beanutils.BeanUtils;

import zzzhc.image.recognize.Filter;

public class FilterSettingDialog extends JDialog {

	private static final long serialVersionUID = 1L;

	private Filter filter;

	private JPanel filterPanel = null;

	private JLabel jLabel = null;

	private JButton jButton = null;

	private boolean doOperate = false;

	/**
	 * @param owner
	 */
	public FilterSettingDialog(Frame owner, Filter filter) {
		super(owner);
		this.filter = filter;
		initialize();
		UIUtil.centerWindow(this);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(400, 300);
		this.setTitle("过滤器参数设置");
		this.getContentPane().add(getFilterPanel(), BorderLayout.CENTER);
	}

	/**
	 * This method initializes filterPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getFilterPanel() {
		if (filterPanel == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 1;
			gridBagConstraints1.gridy = 10;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.gridwidth = 1;
			gridBagConstraints2.fill = GridBagConstraints.NONE;
			gridBagConstraints2.gridy = 10;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.anchor = GridBagConstraints.EAST;
			gridBagConstraints.insets = new Insets(0, 0, 0, 5);
			gridBagConstraints.gridy = 0;
			jLabel = new JLabel();
			jLabel.setText("参数:");
			filterPanel = new JPanel();
			filterPanel.setLayout(new GridBagLayout());
			filterPanel.add(jLabel, gridBagConstraints);
			filterPanel.add(getJButton(), gridBagConstraints2);
			createFilterInput();
			filterPanel.add(getJButton1(), gridBagConstraints1);
		}
		return filterPanel;
	}

	private Map<String, JTextField> textMap = new HashMap<String, JTextField>(); // @jve:decl-index=0:

	private JButton jButton1 = null;

	private void createFilterInput() {
		Class<? extends Filter> cls = filter.getClass();
		Field[] fs = cls.getDeclaredFields();
		int y = 1;
		for (Field f : fs) {
			int m = f.getModifiers();
			if ((m & Modifier.STATIC) != 0) {
				continue;
			}
			String name = f.getName();
			f.setAccessible(true);
			Object o = null;
			try {
				o = f.get(filter);
			} catch (Exception e) {
				e.printStackTrace();
			}
			String value = o == null ? "" : o.toString();
			JLabel label = new JLabel(name);
			JTextField field = new JTextField(value);
			field.setColumns(20);
			textMap.put(name, field);
			GridBagConstraints c = new GridBagConstraints();
			c.gridy = y;
			c.gridx = 0;
			c.insets = new Insets(0, 0, 0, 5);
			c.anchor = GridBagConstraints.EAST;
			filterPanel.add(label, c);

			GridBagConstraints c1 = new GridBagConstraints();
			c1.gridy = y;
			c1.gridx = 1;
			c1.fill = GridBagConstraints.HORIZONTAL;
			c1.anchor = GridBagConstraints.WEST;

			filterPanel.add(field, c1);

			y++;

		}
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("确定");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Field[] fs = filter.getClass().getDeclaredFields();
					Map<String, Object> nv = new HashMap<String, Object>();
					for (Field f : fs) {
						int m = f.getModifiers();
						if ((m & Modifier.STATIC) != 0) {
							continue;
						}
						String name = f.getName();
						JTextField field = textMap.get(name);
						nv.put(name, field.getText());
					}
					try {
						BeanUtils.populate(filter, nv);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
					doOperate = true;
					setVisible(false);
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("取消");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setVisible(false);
				}
			});
		}
		return jButton1;
	}

	public boolean isDoOperate() {
		return doOperate;
	}

}
